package com.qingke.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qingke.annotation.SkipSecurityCheck;
import com.qingke.pojo.Employee;
import com.qingke.pojo.Student;
import com.qingke.service.EmployeeService;
import com.qingke.service.StudentService;

@Controller
@RequestMapping("/user")
public class UserController {
	private static final String STU = "stu";
	private static final String ADMIN = "admin";
	private static final String GUIDER = "guider";
	private static final String INTERVIEWER = "interviewer";
	private static final String INVESTIGATOR = "investigator";
	private static final String TEACHARGE = "teaCharge";
	private static final String TEA = "tea";
	private static final String FAILED = "failed";
	private static Logger logger = Logger.getLogger(UserController.class);
	@Resource
	private EmployeeService employeeService;
	@Resource
	private StudentService studentService;

	@RequestMapping("login")
	@SkipSecurityCheck
	public String loginCheck(HttpServletRequest request, ModelMap modelMap) {
		String userNumber = request.getParameter("username");
		String password = request.getParameter("password");
		String role = request.getParameter("role");
		logger.info("get userNumber=" + userNumber + ",password=" + password + ",role=" + role);
		String name = getRoleValue(role);
		if ("学生".equals(name)) {
			boolean canLogin = studentService.canStudentLogin(userNumber, password);
			if (!canLogin) {
				return "redirect:/loginFail.html";
			}
			Student student = studentService.getStudentByStudentNumber(userNumber);
			modelMap.put("user", student);

			HttpSession se = request.getSession(true);
			se.setAttribute("user", student);
			se.setAttribute("logged", "yes");

			return "redirect:/index-stu.html";
		} else {
			boolean canLogin = employeeService.canEmployeeLogin(userNumber, password, name);
			if (!canLogin) {
				return "redirect:/loginFail.html";
			}
			Employee employee = employeeService.getEmployeeeByEmployeeNumber(userNumber);
			modelMap.put("user", employee);

			HttpSession se = request.getSession(true);
			se.setAttribute("user", employee);
			se.setAttribute("logged", "yes");
			return getURL(role);
		}
	}

	@RequestMapping("getUserMessage")
	@SkipSecurityCheck
	@ResponseBody
	public Map getUserName(HttpServletRequest request, ModelMap modelMap) {
		Map<String, Object> map = new HashMap();
		HttpSession se = request.getSession();
		Object user = se.getAttribute("user");
		if (user instanceof Employee) {
			map.put("name", ((Employee) user).getName());
			map.put("id", ((Employee) user).getId());
		} else if (user instanceof Student) {
			map.put("name", ((Student) user).getName());
			map.put("id", ((Student) user).getId());
		}
		return map;
	}

	public static String getRoleValue(String role) {
		if (role == null) {
			return FAILED;
		}
		switch (role) {
		case STU:
			return "学生";
		case ADMIN:
			return "超级管理员";
		case TEA:
			return "驻班教师";
		case TEACHARGE:
			return "班主任";
		case INTERVIEWER:
			return "面试官";
		case INVESTIGATOR:
			return "调查员";
		case GUIDER:
			return "就业指导老师";
		default:
			return FAILED;
		}
	}

	public static String getURL(String role) {
		if (role == null) {
			return "redirect:/login.html";
		}
		switch (role) {
		case ADMIN:
			return "redirect:/index-admin.html";
		case TEA:
			return "redirect:/index-tea.html";
		case TEACHARGE:
			return "redirect:/index-teaCharge.html";
		case INTERVIEWER:
			return "redirect:/index-interviewer.html";
		case INVESTIGATOR:
			return "redirect:/index-investigator.html";
		case GUIDER:
			return "redirect:/index-guider.html";
		default:
			return "redirect:/login.html";
		}

	}

}
